package com.xtourms.client.rm.ticket;

import java.util.Stack;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.LocationWidget;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.shared.field.TicketField;

public class TicketManagerFrame extends Composite implements ContentFrame{

	private static TicketManagerFrameUiBinder uiBinder = GWT
			.create(TicketManagerFrameUiBinder.class);

	interface TicketManagerFrameUiBinder extends
			UiBinder<Widget, TicketManagerFrame> {
	}

	protected static TicketManagerServiceAsync tmsa = GWT.create(TicketManagerService.class);
	
	@UiField HTMLPanel tmf;
	protected Stack<Widget> widgetManager = new Stack<Widget>();
	private ContentFrame me;
	
	public TicketManagerFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		this.me = this;
		appendContent(new TicketManager(null, me));
	}

	@UiField TextBox ticket_name;
	@UiField LocationWidget location;
	@UiField TextBox ticket_add;
	@UiField Button search;
	
	@UiHandler("search")
	void search(ClickEvent evt){
		String condition;
		JSONObject condObj = new JSONObject();
		condObj.put(TicketField.TICKET_NAME, new JSONString(ticket_name.getValue()));
		condObj.put(TicketField.TICKET_CITY_ID, new JSONNumber(location.getSelectedCityId()));
		condObj.put(TicketField.TICKET_ADD, new JSONString(ticket_add.getValue()));
		condition = condObj.toString();
		search.setEnabled(false);
		tmsa.queryTicket(condition, new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String result) {
				search.setEnabled(true);
				JSONArray ret = (JSONArray)JSONParser.parse(result);
				
				resetContent();
				
				appendContent(new TicketManager(ret, me));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("查询票务服务错误:"+caught.getMessage());
				search.setEnabled(true);
			}
		});
	}
	
	@UiHandler("reset")
	void reset(ClickEvent evt){
		ticket_name.setValue("");
		location.reset();
		ticket_add.setValue("");
	}
	
	public void appendContent(Widget next){
		if(0 != widgetManager.size()){	
			Widget per = widgetManager.peek();
			per.removeFromParent();
		}
		widgetManager.push(next);
		tmf.add(next, "tm_content");
	}
	
	public void resumeContent(){
		if(1 < widgetManager.size()){
			Widget cur = widgetManager.pop();
			cur.removeFromParent();
			cur = widgetManager.peek();
			tmf.add(cur, "tm_content");
		}
	}
	
	public void resetContent() {
		if(!widgetManager.isEmpty()){
			Widget cur = widgetManager.pop();
			cur.removeFromParent();
		}
		widgetManager.clear();
	}
}
